import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class BufferedImageTool {

    public static void resize(String imgPath, double factor) throws IOException {

        File file = new File(imgPath);
        BufferedImage bufferedImage = ImageIO.read(file);

        int width = (int) (bufferedImage.getWidth() * factor);
        int height = (int) (bufferedImage.getHeight() *factor);

        BufferedImage bufferedImage1 = resize(bufferedImage, width, height);


        File outfile = new File("image.jpg");
        ImageIO.write(bufferedImage1, "jpg", outfile);
    }


    /**
     * https://blog.csdn.net/zhongguowangzhan/article/details/114977785
     */
    public static BufferedImage resize(BufferedImage img, int newW, int newH) {
        Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
        BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);

        Graphics2D g2d = dimg.createGraphics();
        g2d.drawImage(tmp, 0, 0, null);
        g2d.dispose();

        return dimg;
    }

}
